﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CuitContextFactory.cs" company="">
//   
// </copyright>
// <summary>
//   The cuit context factory.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UITest.CUIT.CuitContext
{
    using System;

    /// <summary>
    /// The cuit context factory.
    /// </summary>
    public class CuitContextFactory
    {
        #region Static Fields

        private static readonly CuitContextFactory Instance = new CuitContextFactory();

        #endregion

        #region Properties

        private CuitContext CuitContext { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     The dispose.
        /// </summary>
        public static void Dispose()
        {
            Instance.CuitCleanup();
        }

        /// <summary>
        ///     The get Cuit context.
        /// </summary>
        /// <returns>
        ///     The <see cref="CuitContext" />.
        /// </returns>
        /// <exception cref="NullReferenceException">
        /// </exception>
        public static CuitContext GetCuitContext()
        {
            if (Instance.CuitContext == null)
            {
                throw new NullReferenceException("You MUST use CuitContextFactory.New() before calling this method");
            }

            return Instance.CuitContext;
        }

        /// <summary>
        /// The new.
        /// </summary>
        public static void New()
        {
            Instance.CuitCleanup();
            Instance.CuitContextInit();
        }

        #endregion

        #region Methods

        private void CuitCleanup()
        {
            if (this.CuitContext != null)
            {
                this.CuitContext.Dispose();
            }
        }

        private void CuitContextInit()
        {
            if (this.CuitContext != null)
            {
                this.CuitContext.Dispose();
            }

            this.CuitContext = new CuitContext();
        }

        #endregion
    }
}